﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml.Linq;
using TwilioSharp.Code.Filters;
using TwilioSharp.Code.Twilio;
using TwilioSharp.Code.Twilio.Types;

namespace TwilioSharp.Areas.TwilioListener.Controllers
{
    public class VoiceController : Controller
    {
        //
        // GET: /TwilioListener/Voice/

        public ActionResult Index()
        {
            return View();
        }

        [TwilioFilter]
        public void StopCallingMe()
        {
            Trace.Write("Begin", "TwilioListener.Voice.StopCallingMe");
            if (int.Parse(Request["Digits"]) != 1) return;
            
            var callRequest = (CallRequest) this.HttpContext.Items["CallRequest"];
            var output = new ResponseType()
                             {
                                 Items = new object[]
                                             {
                                                 new SayVerb()
                                                     {
                                                         Value =
                                                             "you asked to not be called again at " + callRequest.Called +
                                                             ", we won't thanks."
                                                     }
                                             }
                             }.ToXml();
            Trace.Write("End", "TwilioListener.Voice.StopCallingMe");
            Response.Write(output);
        }

        [TwilioFilter]
        public void OutBoundCallInstructions()
        {
            var output = new ResponseType()
                             {
                                 Items = new object[]
                                             {

                                                 new SayVerb() {Value = "hello world"},
                                                 new PlayVerb() {Value = "http://demo.twilio.com/hellomonkey/monkey.mp3"},
                                                 new GatherVerb() {
                                                     numDigits = 1,
                                                     action = "StopCallingMe",
                                                     method = MethodValues.POST,
                                                     Items = new object[]
                                                                 {
                                                                 new SayVerb(){Value="To not be called again, press 1."}
                                                                 }
                                                 }


                                             }

                             }.ToXml();
            Response.Write(output);
        }

    }
}
